﻿using System.Collections.Generic;
using System.IO;
using JPR.TunelProxy.DataObjects;
using JPR.TunelProxy.Interfaces;

namespace JPR.TunelProxy.Proxy.Implementations
{
    public class HttpParser : IHttpParser
    {
        private Stream _stream;
        private bool _headerCompleted;
        private int _streamPositionMarker;
        private int _contentLength;
        private int _contentByteReaded;
        private HttpRequestData _requestData;
        private HttpResponseData _responseData;

        public HttpParser()
        {
            _stream = new MemoryStream();
            _headerCompleted = false;
            _streamPositionMarker = 0;
            _contentLength = -1;
            _contentByteReaded = 0;
            _requestData = null;
            _responseData = null;
        }

        public void AddData(byte[] data)
        {
            AddData(data, 0, data.Length);
        }

        public void AddData(byte[] data, int offset, int count)
        {
            _stream.Seek(0, SeekOrigin.End);
            _stream.Write(data, offset, count);
        }

        public bool IsHeaderCompleted
        {
            get { return _headerCompleted; }
        }

        public bool IsHttpCompleted
        {
            get { return IsHeaderCompleted && (_contentLength == _contentByteReaded); }
        }

        public IDictionary<string, string> GetHttpHeaders()
        {
            throw new System.NotImplementedException();
        }

        public int ContentLength { get; private set; }

        public Stream GetContent()
        {
            throw new System.NotImplementedException();
        }

        public string GetCommandOrStatusLine()
        {
            throw new System.NotImplementedException();
        }

        public bool IsRequest
        {
            get { return _requestData != null; }
        }

        public bool IsResponse
        {
            get { return _responseData != null; }
        }

        public HttpRequestData GetHttpRequestData()
        {
            throw new System.NotImplementedException();
        }

        public HttpResponseData GetHttpResponseData()
        {
            throw new System.NotImplementedException();
        }

        public IHttpData GetHttpData()
        {
            if (_requestData != null)
            {
                return GetHttpRequestData();
            }
            if (_responseData != null)
            {
                return GetHttpResponseData();
            }
            return null;
        }
    }
}
